package org.example.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@MapperScan("cn.edu.zut.soft.graduate.mapper") // 扫描mapper文件夹
@EnableTransactionManagement
//@EnableConfigurationProperties(GraduateEnumHandler.class)
//手动调用该配置类中方法的话将会返回非单例，但更快，一般也没人手调
@Configuration(proxyBeanMethods = false)
public class MyBatisPlusConfig {

    //注册乐观锁插件
    @Bean
    public MybatisPlusInterceptor optimisticLockerInnerInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

    // 配置分页插件
    @Bean
     public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

//    配置用于批量操作的sqlSession对象
    @Bean
    public SqlSession batchSqlSession(SqlSessionFactory sqlSessionFactory){
        return sqlSessionFactory.openSession(ExecutorType.BATCH);
    }

//    配置用于批量操作的Mapper对象
//    @Bean
//    public IdentityMapper batchIdentityMapper(@Qualifier("batchSqlSession") SqlSession sqlSession){
//        return sqlSession.getMapper(IdentityMapper.class);
//    }

}
